<?php
namespace app\app\controller;
use think\Db;
class Common{
    public function __construct()
    {   //如果bug为真
        $debug = true;
        if($debug){
            header("Access-Control-Allow-Origin: *");//header头
            if(!is_dir(APP_PATH."/Debug")){
                mkdir(APP_PATH."/Debug");
            }
            if(!is_file(APP_PATH."/Debug/record")){
                file_put_contents(APP_PATH."/Debug/record",'');
            }
            $url_str = explode("?",$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
            $url = $url_str[0];
            $request = '';
            foreach ($_REQUEST as $k => $v){
                $request .= $k."=".$v."&";
            }
            $request = substr($request,0,strlen($request)-1);
            if($request !='')
                $url .= "?".$request;
            $content = "<hr>\r\n<div style='padding: 10px;'>\r\n<a target='_blank' href='http://".$url."'>http://".$url."</a></div>\r\n<pre style='font-size: 14px; margin-left: 10px;'>\r\n数据参数：\r\n".print_r($_REQUEST,true)."\r\n文件参数：\r\n".print_r($_FILES,true)."</pre>\r\n</div>\r\n<div style='margin-left: 10px; font-size: 13px'><span>".$_SERVER['HTTP_USER_AGENT']." ".$_SERVER['REMOTE_ADDR']."</span><span style='float: right; margin-right: 10px;'>".date("Y-m-d H:i:s") ."</span></div></div>\r\n<hr>\r\n";
            if(filesize(APP_PATH."/Debug/record") > 1048576){
                file_put_contents(APP_PATH."/Debug/record","");
            }
            file_put_contents(APP_PATH."/Debug/record",$content.file_get_contents(APP_PATH."/Debug/record"));
        }

        function base64_set($base)
        {
            $result = '';
            for($i=strlen($base)-1;$i>=0;$i--){
                $result .= $base[$i];
            }
            return $result;
        }

        //请求安全稍稍加密,逆天少侠独创
        function token_encode($id)
        {
            $time = time();
            return base64_set(base64_encode(($time^$id)."_".$time));
        }

        function token_decode($token)
        {
            $token = base64_decode(base64_set($token));
            if($token == ''){
                die('token inexistence');
            }
            if(!strpos($token,"_")){
                die('token error');
            }
            $id_array = explode("_",$token);
            if(count($id_array) < 2){
                die('token error');
            }
            return (int)$id_array[0]^(int)$id_array[1];
        }

        function upload()
        {
            $config = Db::table('material_config')->where('id',1)->find();
            config([
                'UPLOAD_SITEIMG_QINIU'=>[
                    'maxSize'          => 5 * 1024 * 1024,//文件大小
                    'rootPath'         => './',
                    'saveName'         => ['uniqid', ''],
                    'driver'           => 'Qiniu',
                    'driverConfig' => [
                        'secretKey' => $config['secretKey'],
                        'accessKey' => $config['accessKey'],
                        'domain'    => $config['domain'],
                        'bucket'    => $config['bucket'],
                    ],
                ],
                'file_size'=>$config['file_size']
            ]);
            $setting = config('UPLOAD_SITEIMG_QINIU');
            $Upload = new \think\Upload($setting);
            $info = $Upload->upload($_FILES);
            return $info;
        }

    }

    //计算经纬度
    public function get_lat_lng($km)
    {
        $pi = '0.005'; //1km
        $result = $km * $pi?$km * $pi:1;
        return (string)$result;
    }

    //计算距离
    public function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $earthRadius = 6367000; //approximate radius of earth in meters

        $lat1 = ($lat1 * pi()) / 180;
        $lng1 = ($lng1 * pi()) / 180;

        $lat2 = ($lat2 * pi()) / 180;
        $lng2 = ($lng2 * pi()) / 180;

        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;

        return round($calculatedDistance);
    }


}